<?php
namespace app\wechat\controller;

use app\common\enums\ResultEnum;
use app\common\utils\ResultVOUtil;
use think\facade\Log;
use app\common\model\Category;
use app\common\model\Article;
use app\common\model\Video;

class Blend
{
    public function index(){
        $cate = new Category();
        $article = new Article();
        $video = new Video();
        try {
            $data['cate'] = $cate->where('pid=0')->limit(10)->select();
            $articleList = $article->limit(5)->field('id as aid,id,title,abstract,thumb,update_time,click')->select();
            $videoList = $video->limit(5)->field('id as aid,id,title,abstract,thumb,update_time,click')->select();
            $data['blend'] = array_merge($articleList->toArray(),$videoList->toArray());
        } catch (\Exception $e) {
            Log::record('[发现列表]首屏:'.$e->getMessage(),'error');
            return ResultVOUtil::error(ResultEnum::FIND_ERROR);
        }
        return ResultVOUtil::success($data);
    }
    
    public function list($search='',$length=5,$page=1,$cate=0) {
        $article = new Article();
        $video = new Video();
        $where = [];
        
        if ( !empty($search) ) {
            
            $where[] = ['title','like','%'.$search.'%'];
        }
        
        $cateModel = new Category();
        
        if($cate){
            try {
                $cid = $cateModel->where(['pid'=>$cate])->column('id');
            } catch (\Exception $e) {
            }
            $where[] = ['cid','in',$cid];
        }
        try {
            $articleList = $article->where($where)->field('id as aid,id,title,abstract,thumb,update_time,click')->paginate($length,false,[
                'type'     => 'bootstrap',
                'var_page' => 'page',
                'page'=>$page,
            ]);
            $videoList = $video->where($where)->field('id as vid,id,title,abstract,thumb,update_time,click')->paginate($length,false,[
                'type'     => 'bootstrap',
                'var_page' => 'page',
                'page'=>$page,
            ]);
            
        } catch (\Exception $e) {
            Log::record('[发现列表]:'.$e->getMessage(),'error');
            return ResultVOUtil::error(ResultEnum::FIND_ERROR);
        }
       // dump($articleList);
        return ResultVOUtil::success(array_merge($articleList->toArray()['data'],$videoList->toArray()['data']));
        
    }
}

